<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('encode_for_uri'))
{
	function encode_for_uri($data)
	{
		$data = base64_encode($data);
		$data = str_replace("=", "", $data);
		$data = str_replace("+", "-", $data);
		$data = str_replace("/", "_", $data);
		return $data;
	}
}

if ( ! function_exists('decode_from_uri'))
{
	function decode_from_uri($data)
	{
		$data = str_replace("_", "/", $data);
		$data = str_replace("-", "+", $data);
		$data = base64_decode($data);
		return $data;
	}
}

if ( ! function_exists('change_case'))
{
	function change_case($string, $case)
	{
		if ($case == CASE_LOWER) {
			return strtolower($string);
		} else if ($case == CASE_UPPER) {
			return strtoupper($string);
		} else {
			return $string;
		}
	}
}

if (!function_exists('substr_compare')) {
    function substr_compare($main_str, $str, $offset, $length = NULL, $case_insensitivity = false) {
        $offset = (int) $offset;

        // Throw a warning because the offset is invalid
        if ($offset >= strlen($main_str)) {
            trigger_error('The start position cannot exceed initial string length.', E_USER_WARNING);
            return false;
        }

        // We are comparing the first n-characters of each string, so let's use the PHP function to do it
        if ($offset == 0 && is_int($length) && $case_insensitivity === true) {
            return strncasecmp($main_str, $str, $length);
        }

        // Get the substring that we are comparing
        if (is_int($length)) {
            $main_substr = substr($main_str, $offset, $length);
            $str_substr = substr($str, 0, $length);
        } else {
            $main_substr = substr($main_str, $offset);
            $str_substr = $str;
        }

        // Return a case-insensitive comparison of the two strings
        if ($case_insensitivity === true) {
            return strcasecmp($main_substr, $str_substr);
        }

        // Return a case-sensitive comparison of the two strings
        return strcmp($main_substr, $str_substr);
    }
}

if (!function_exists('str_from_template')) {
	function str_from_template($template, $data) {
		$offset = 0;
		do {
			$new_template = $template;
			$changed = false;
			do {
				if ($offset>strlen($template)) $offset = strlen($template);
				$co = strpos($template, "{", $offset);
				$cc = strpos($template, "}", $offset);
							
				$r = false;
				
				if (($co !== false) && ($cc !== false)) {
					if ($cc >= $co) {
						$r = substr($template, $co+1, $cc-$co-1);
						$offset = $cc+1;
					} else {
						$r = true;
						$offset = $co;
					}
				}
													
				if (($r !== false) && (isset($data[$r]))) {
					$new_template = str_replace("{".$r."}", $data[$r], $new_template);
				}
			} while ($r !== false);

			if (strcmp($template, $new_template) != 0) {
				$template = $new_template;
				$changed = true;
			}
		} while ($changed);
			
		return $template;
	}
}

/* End of file MY_string_helper.php */
/* Location: ./system/application/helpers/MY_string_helper.php */